It shows how to get images via callback. The major steps include creating a handle, turning on the camera, registering a callback, starting grabbing images, getting image information via callback, stopping grabbing images, turning off the camera, and destroying the handle.
11 currentsystem = platform.system()
12 if currentsystem ==
'Windows':
13 sys.path.append(os.path.join(os.getenv(
'MVCAM_COMMON_RUNENV'),
"Samples",
"Python",
"MvImport"))
15 sys.path.append(os.path.join(
"..",
"..",
"MvImport"))
17 from MvCameraControl_class
import *
21 if sys.version_info[0] < 3:
23 input_func = raw_input
31 Safely decode a string from a ctypes character array. 32 Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments. 34 byte_str = memoryview(ctypes_char_array).tobytes()
37 null_index = byte_str.find(b
'\x00')
39 byte_str = byte_str[:null_index]
42 for encoding
in [
'gbk',
'utf-8',
'latin-1']:
44 return byte_str.decode(encoding)
45 except UnicodeDecodeError:
49 return byte_str.decode(
'latin-1', errors=
'replace')
53 FrameInfoCallBack2 =
fun_ctype(
None, POINTER(MV_FRAME_OUT), c_void_p, c_bool)
55 stFrame = cast(pstFrame, POINTER(MV_FRAME_OUT)).contents
57 print (
"get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (stFrame.stFrameInfo.nWidth, stFrame.stFrameInfo.nHeight, stFrame.stFrameInfo.nFrameNum))
60 if bAutoFree ==
False :
62 User = ctypes.cast(pUser, ctypes.py_object).value
63 User.MV_CC_FreeImageBuffer(stFrame)
65 print (
"user is null, invalid")
71 if __name__ ==
"__main__":
74 MvCamera.MV_CC_Initialize()
76 SDKVersion = MvCamera.MV_CC_GetSDKVersion()
77 print (
"SDKVersion[0x%x]" % SDKVersion)
79 deviceList = MV_CC_DEVICE_INFO_LIST()
80 tlayerType = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE
81 | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE)
84 ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
86 print (
"enum devices fail! ret[0x%x]" % ret)
89 if deviceList.nDeviceNum == 0:
90 print (
"find no device!")
93 print (
"find %d devices!" % deviceList.nDeviceNum)
95 for i
in range(0, deviceList.nDeviceNum):
96 mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
97 if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE
or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
98 print (
"\ngige device: [%d]" % i)
99 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
100 print (
"device model name: %s" % strModeName)
101 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
102 print(
"device serial number: %s" % strSerialNumber)
103 nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
104 nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
105 nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
106 nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
107 print (
"current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
108 elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
109 print (
"\nu3v device: [%d]" % i)
110 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
111 print (
"device model name: %s" % strModeName)
113 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
114 print (
"device serial number: %s" % strSerialNumber)
115 elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
116 print (
"\nCML device: [%d]" % i)
117 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
118 print (
"device model name: %s" % strModeName)
120 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber)
121 print (
"device serial number: %s" % strSerialNumber)
122 elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
123 print (
"\nCXP device: [%d]" % i)
124 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
125 print (
"device model name: %s" % strModeName)
127 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber)
128 print (
"device serial number: %s" % strSerialNumber)
129 elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
130 print (
"\nXoF device: [%d]" % i)
131 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
132 print (
"device model name: %s" % strModeName)
134 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber)
135 print (
"device serial number: %s" % strSerialNumber)
137 nConnectionNum =
input_func(
"please input the number of the device to connect:")
139 if int(nConnectionNum) >= deviceList.nDeviceNum:
140 print (
"intput error!")
147 stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
149 ret = cam.MV_CC_CreateHandle(stDeviceList)
151 raise Exception (
"create handle fail! ret[0x%x]" % ret)
154 ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
156 raise Exception (
"open device fail! ret[0x%x]" % ret)
159 if stDeviceList.nTLayerType == MV_GIGE_DEVICE
or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
160 nPacketSize = cam.MV_CC_GetOptimalPacketSize()
161 if int(nPacketSize) > 0:
162 ret = cam.MV_CC_SetIntValue(
"GevSCPSPacketSize",nPacketSize)
164 print (
"Warning: Set Packet Size fail! ret[0x%x]" % ret)
166 print (
"Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
169 ret = cam.MV_CC_SetEnumValue(
"TriggerMode", MV_TRIGGER_MODE_OFF)
171 raise Exception (
"set trigger mode fail! ret[0x%x]" % ret)
174 ret = cam.MV_CC_RegisterImageCallBackEx2(CALL_BACK_FUN2, ctypes.py_object(cam),
True)
176 raise Exception (
"register image callback fail! ret[0x%x]" % ret)
179 ret = cam.MV_CC_StartGrabbing()
181 raise Exception (
"start grabbing fail! ret[0x%x]" % ret)
183 print (
"press Enter key to stop grabbing.")
187 ret = cam.MV_CC_StopGrabbing()
189 raise Exception (
"stop grabbing fail! ret[0x%x]" % ret)
192 ret = cam.MV_CC_CloseDevice()
194 raise Exception (
"close deivce fail! ret[0x%x]" % ret)
197 cam.MV_CC_DestroyHandle()
199 except Exception
as e:
201 cam.MV_CC_CloseDevice()
202 cam.MV_CC_DestroyHandle()
205 MvCamera.MV_CC_Finalize()